<template>
	<cu-custom bgColor="bg-gradual-blue">
		个人信息
		<template #right>
			<view class="margin-right" @click="toProfile">返回报名</view>
		</template>
	</cu-custom>
	<view class="profile-content">
		<view class="padding radius text-left shadow-blur bg-blue margin-xs">
			<view class="text-Abc">
				报名成功时，这里的信息会同步更新。所以，没有特殊情况，无需在这里修改信息。
			</view>
		</view>
		<form class="margin-top app-form">
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>基本信息
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">工号</view>
				<input disabled="true" v-model="model.Code" name="input" class="text-right" />
			</view>
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input disabled="true" v-model="model.RealName" name="input" class="text-right" />
			</view>
			<view class="cu-form-group">
				<view class="title">所在部门</view>
				<input disabled="true" :value="departName" name="input" class="text-right" />
			</view>
			<view class="cu-form-group">
				<view class="title required">性别</view>
				<input disabled="true" v-model="model.Gender" name="input" class="text-right" />
			</view>
			<view class="cu-form-group">
				<view class="title required">是否在编</view>
				<input disabled="true" :value="model.IsPayroll ? '是' : '否'" name="input" class="text-right" />
			</view>
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>个人信息
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title required">手机号</view>
				<input v-model="model.Mobile" name="input" class="text-right" />
			</view>
			<view class="cu-form-group">
				<view class="title required">邮箱</view>
				<input v-model="model.Email" name="input" class="text-right" />
			</view>
			<view class="cu-form-group" v-if="!model.IsPayroll">
				<view class="title required">证件类型</view>
				<dict-select v-model:value="model.CodeType" dict="证件类型" prefix="" remove="教工号" />
			</view>
			<view class="cu-form-group" v-if="!model.IsPayroll">
				<view class="title required">证件号</view>
				<input v-model="model.UserName" name="input" class="text-right" />				
			</view>
			<view class="cu-form-group" v-if="!model.IsPayroll">
				<view class="title required">开户行</view>
				<input v-model="model.BankName" name="input" class="text-right" />				
			</view>
			<view class="cu-form-group" v-if="!model.IsPayroll">
				<view class="title required">银行卡号</view>
				<input v-model="model.CardNumber" name="input" class="text-right" />				
			</view>
			<button class="margin bg-blue lg" @click="onBasicSubmit">提交</button>
			<button class="margin bg-gray lg" @click="back">注销</button>
			<view class="fixed">fixed</view>
		</form>
	</view>
</template>

<script setup>
	import { ref, reactive, inject, watch, watchEffect, onMounted } from 'vue'
	import app from '@/app';
	import axios from '@/axios'
	import _ from 'lodash'
	import { logout } from '@/configs/useCurrentUser'
	
	const departName = ref("");
	const form = ref();
	const empty = {
	    "RealName": "",
	    "Code": "",
	    "DisplayOrder": 0,
	    "Mobile": "",
	    "Email": "",
	    "UserName": "",
	    "OpenId": "",
	    "Avatar": "",
	    "Level": 3,
	    "RootDepartId": app.GUID_EMPTY,
	    "LockCount": 0,
	    "LockTo": "",
	    "AuditStatus": 1,
	    "UserSource": "后台添加",
	    "Gender": "男",
	    "CodeType": "居民身份证",
	    "BankName": "",
	    "CardNumber": "",
	    "IsPayroll": false,
	    "ID": app.GUID_EMPTY
	};
	const model = reactive({ ...empty });
	
	onMounted(() => {
		let commonRole = app.currentUser.Roles.find(e => e.Name === "普通教师");
		if (commonRole) {
		    departName.value = commonRole.DepartName;
		}
	    loadUser();
	});
	
	async function loadUser() {
	    let msg = await axios.post("/api/users/GetProfile");
	    if (!msg.success) {
	        alert(msg.msg);
	        return;
	    }
	    Object.assign(model, msg.data);
	}
	
	async function back () {
		await logout();
		uni.redirectTo({
			url: "/pages/login/index"
		})
	}
	
	// -- 登录信息 -- //
	const errors = ref([]);
	const loading = ref(false);
	const formRef = ref();
	async function onBasicSubmit() {
	    try {
	        let msg = await axios.post("/api/users/SaveProfile", model);
	        if (msg.success) {
	            Object.assign(model, msg.data);
	            errors.value = [];
	            alert("保存成功");
	        } else {
				alert(msg.errorStrings);
	        }
	    } catch {} finally {
	        loading.value = false;
	    }
	}
	
	function toProfile() {
		uni.redirectTo({
			url: "/pages/index/index"
		})
	}
</script>

<style>
	.fixed {
		font-size: 0;
	}
</style>